all:

SHELL       := @SHELL@

# Support out-of-tree builds
srcdir      := @srcdir@
VPATH       := @srcdir@

prefix      := @prefix@
exec_prefix := @exec_prefix@
datarootdir := @datarootdir@
bindir      := @bindir@
datadir     := @datadir@
sysconfdir  := @sysconfdir@
libdir      := @libdir@
sbindir     := @sbindir@
localstatedir := @localstatedir@
CXX      := @CXX@
# CXXFLAGS are for the main binary, so don't use them here, use MODFLAGS instead
MODFLAGS := @DEFS@ @CPPFLAGS@ @MODFLAGS@ -I$(srcdir)/.. -I..
MODLINK  := @MODLINK@
LDFLAGS  := @LDFLAGS@
# LIBS are not and should not be used in here.
# The znc binary links already against those.
# LIBS     := @LIBS@
PERL_ON  := @PERL@
PERL     := @PERL_BINARY@
PYCFG    := @PYTHONCFG_BINARY@
SWIG     := @SWIG_BINARY@
MODDIR   := @MODDIR@
DATADIR  := @DATADIR@
LIBZNC   := @LIBZNC@
LIBZNCDIR:= @LIBZNCDIR@
INSTALL         := @INSTALL@
INSTALL_PROGRAM := @INSTALL_PROGRAM@
INSTALL_SCRIPT  := @INSTALL_SCRIPT@
INSTALL_DATA    := @INSTALL_DATA@

TCL_FLAGS:= @TCL_FLAGS@

ifneq "$(V)" ""
VERBOSE=1
endif
ifeq "$(VERBOSE)" ""
Q=@
E=@echo
else
Q=
E=@\#
endif

ifneq "$(LIBZNC)" ""
LDFLAGS += -L.. -lznc -Wl,-rpath,$(LIBZNCDIR)
endif

CLEAN    :=

FILES    := $(notdir $(wildcard $(srcdir)/*.cpp))
# If extra is enabled
ifeq "@EXTRA@" "yes"
FILES    += $(addprefix extra/, $(notdir $(wildcard $(srcdir)/extra/*.cpp)))
endif

include $(srcdir)/modperl/Makefile.inc
include $(srcdir)/modpython/Makefile.inc
include $(srcdir)/modtcl/Makefile.inc

FILES    := $(basename $(FILES))

ifeq "@NOSSL@" "1"
FILES    := $(foreach file, $(FILES), \
	$(if $(shell grep REQUIRESSL $(srcdir)/$(file).cpp), \
		, \
		$(basename $(file)) \
	))
endif

ifeq "@SASL@" ""
FILES    := $(shell echo $(FILES) | sed -e "s:extra/saslauth::")
endif
saslauthFLAGS := -lsasl2

ifeq "@CHARSET@" ""
FILES    := $(shell echo $(FILES) | sed -e "s:extra/charset::")
endif
charsetFLAGS := @LIBICONV@

TARGETS  := $(addsuffix .so, $(FILES))

CLEAN    += *.so extra/*.so

.PHONY: all clean install install_metadirs create_install_dir uninstall

all: $(TARGETS)

install: all create_install_dir install_metadirs
	$(INSTALL_PROGRAM) $(TARGETS) $(DESTDIR)$(MODDIR)

create_install_dir:
	mkdir -p $(DESTDIR)$(MODDIR)
	mkdir -p $(DESTDIR)$(DATADIR)
	rm -rf $(DESTDIR)$(MODDIR)/*.so

install_metadirs: create_install_dir
	for a in $(srcdir)/*; do \
		d=$$(echo $$a | sed -e "s:$(srcdir)/::g;s:modperl::;s:modpython::"); \
		if [ -d $$a ] && [ -f $${d}.so ]; then \
			cp -Rp $$a $(DESTDIR)$(DATADIR); \
		fi \
	done

clean:
	rm -rf $(CLEAN)

%.so: %.cpp Makefile
	@mkdir -p .depend
	@mkdir -p extra
	$(E) Building $(if $(filter %extra/,$(dir $<)),extra )module $(notdir $(basename $@))...
	$(Q)$(CXX) $(MODFLAGS) $(LDFLAGS) $(MODLINK) -o $@ $< $($(notdir $(basename $@))FLAGS) -MMD -MF .depend/$(notdir $@).dep

uninstall:
	# Yes, we are lazy, just remove everything in there
	rm -rf $(DESTDIR)$(MODDIR)/*
	rm -rf $(DESTDIR)$(DATADIR)/*
	rmdir $(DESTDIR)$(MODDIR)
	rmdir $(DESTDIR)$(DATADIR)

-include $(wildcard .depend/*.dep)
